package com.sun.hui_location;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/** 位置权限申请 */
public class LocationPermission {
    private static final AtomicInteger RequestCodeNum = new AtomicInteger();
    private static final Map<String, Integer> RequestCodeMap = new HashMap<>();
    public Activity mContext;

    LocationPermission(Activity activity){
        this.mContext = activity;
    }
    //权限监测
    public void checkPermission(){
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        //直接定位操作


        
    }

    //判断是否有权限
    public boolean checkPermissions(String permission) {
        int permissionState = ActivityCompat.checkSelfPermission(mContext, permission);
        return permissionState == PackageManager.PERMISSION_GRANTED;
    }

    public void requestPermissions(String permission) {
        Integer code = RequestCodeMap.get(permission);
        if (code == null) {
            code = RequestCodeNum.incrementAndGet();
            RequestCodeMap.put(permission, code);
        }
        ActivityCompat.requestPermissions( mContext, new String[]{ permission },
                code);
    }

}
